/**
 * 
 */
package formulavendasweb.managedbean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import formulavendasweb.util.Constant;

import br.com.formulavendas.businessusercase.ManterUsuario;
import br.com.formulavendas.domainmodel.Usuario;

/**
 * @author Fernando
 *
 */

@RequestScoped
@ManagedBean
public class LoginMB extends AbstractMB {

	 	@ManagedProperty (value = UsuarioMB.INJECTION_NAME)
	    private UsuarioMB userMB;
	 
	    private String login;
	    private String password;
	 
	    public String getLogin() {
	        return login;
	    }
	 
	    public void setLogin(String login) {
	        this.login = login;
	    }
	 
	    public String getPassword() {
	        return password;
	    }
	 
	    public void setPassword(String password) {
	        this.password = password;
	    }
	 
	    public String logar() {
	        ManterUsuario userFacade = new ManterUsuario();
	 
	        Usuario user = userFacade.autenticar(login, password);
	 
	        if(user != null){
	            userMB.setUser(user);
	            FacesContext context = FacesContext.getCurrentInstance();
	            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
	            request.getSession().setAttribute("user", user);
	            return Constant.HOME_PAGE;
	        }
	 
	        displayErrorMessageToUser("Verifique sua Senha e/ou Login.");
	 
	        return null;
	    }
	 
	    public void setUserMB(UsuarioMB userMB) {
	        this.userMB = userMB;
	    }   
	
}
